package com.onemore.task.model;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.voovan.tools.log.Logger;

import com.alibaba.fastjson.JSONObject;
import com.onemore.task.JdbcGlobal;

public class ModelShangpin extends JdbcGlobal{
	public final static String goods_field[] = new String[] {"SPDM","SPMC","ZJF","DWMC","FJSX1",
    		"FJSX2","FJSX3","FJSX4","FJSX5","FJSX6","FJSX7","FJSX8","FJSX9","FJSX10","FJSX11","FJSX12"
    		,"FJSX13","FJSX14","FJSX15","FJSX16","BZJJ","JJ1","JJ2","BZSJ","SJ1","SJ2","SJ3","SJ4",
    		"PIC","BZDW","SPTM",
    		"BZHU","TZSY","BYZD1","BYZD2","BYZD3","BYZD4","BYZD5","BYZD6","BYZD7","BYZD8",
    		"BYZD9","BYZD10","BYZD14","BYZD15","BYZD16","BYZD17","BYZD18","BYZD19","BYZD20","BYZD21",
    		"BYZD22","BYZD23","BYZD24","BYZD25","ONLINE"};
    /**
     * 插入商品
     * @param json
     * @return
     * @throws SQLException 
     */
    static public boolean insert_goods(JSONObject json) throws SQLException
    {
    	String fieldStr = "";
    	String valuesStr = "";
    	String updateStr = "";

    	List<String> listFields = new ArrayList<String>();

    	int i = 0;

    	boolean isHas = false;
    	if(search("SHANGPIN", goods_field[0]+"='"+json.getString(goods_field[0])+"'")) {
    		setDefault(json,"BYZD14",df.format(new Date()));//建档
    		isHas = true;
    	}else {
    		setDefault(json,"BYZD15",df.format(new Date()));//修档
    		isHas = false;
    	}
    	for(i=0;i<goods_field.length;i++) {
    		if(json.containsKey(goods_field[i]))
    			listFields.add(goods_field[i]);
    	}
    	for(i=0;i<listFields.size()-1;i++) {
    		fieldStr += listFields.get(i)+",";
    		valuesStr += "'"+json.getString(listFields.get(i))+"',";
    		updateStr += listFields.get(i)+"='"+json.getString(listFields.get(i))+"',";
    	}
		fieldStr += listFields.get(i);
		valuesStr += "'"+json.getString(listFields.get(i))+"'";
		updateStr += listFields.get(i)+"='"+json.getString(listFields.get(i))+"'";
		//delete("SHANGPIN",goods_field[0]+"='"+json.getString(goods_field[0])+"'");
    	String sql = "";
		if(isHas) {
			updateDate("SHANGPIN",goods_field[0]+"='"+json.getString(goods_field[0])+"'",updateStr);
		}else {
	        //查询测试
	        sql = "insert into SHANGPIN ("+fieldStr+") values("+valuesStr+")";
	        Map<String, Object> map = null;
			Logger.debug(sql);
			getJdbcOperate().update(sql);
		}
		return true;
    }
    static public Float getBZJJ(String spdm) throws SQLException
    {
        String sql = "SELECT BZJJ FROM SHANGPIN WHERE SPDM='"+spdm+"'";
        Map<String, Object> map = null;
        map = getJdbcOperate().queryMap(sql);
        Object ob = map.get("BZJJ");
        if(ob == null)
        	return 0f;
        else
        	return Float.parseFloat(ob.toString());
    }
    static public boolean isExist(String SPDM) throws SQLException
	{
		if(search("SHANGPIN", "SPDM='"+SPDM+"'")){
    		return true;
    	}
		return false;
	}
    static public boolean isValid(String SPDM) throws SQLException
	{
		if(search("SHANGPIN", "SPDM='"+SPDM+"' and TZSY!=1")){
    		return true;
    	}
		return false;
	}
}
